<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="createUserName">
        <el-input
          v-model="queryParams.createUserName"
          placeholder="举报用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="举报用户名" align="center" prop="createUserName" />
      <el-table-column label="举报类型" align="center" prop="reportType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.report_type" :value="scope.row.reportType"/>
        </template>

      </el-table-column>
      <el-table-column label="举报详情" align="center" prop="reportInfo" show-overflow-tooltip="true"/>
      <el-table-column label="举报截图" align="center" prop="reportScreenshot">
        <template slot-scope="scope">
            <image-preview :src="scope.row.reportScreenshot" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="举报文章" align="center" prop="articleTitle" />
      <el-table-column label="举报时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="openDetail(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="changeStatus(scope.row)"
          >{{scope.row.hidden=='0'?'隐藏':'开放'}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文章举报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文章id" prop="articleId">
          <el-input v-model="form.articleId" placeholder="请输入文章id" />
        </el-form-item>
        <el-form-item label="用户id" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="举报详情" prop="reportInfo">
          <el-input v-model="form.reportInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="举报截图" prop="reportScreenshot">
          <el-input v-model="form.reportScreenshot" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <Detail :detailVis.sync="detailVis" :detailObj="detailObj">
    </Detail>
  </div>
</template>

<script>
import { listReport, getReport, delReport, addReport, updateReport } from "@/api/system/report";
import { getArticle,hideArticle} from "@/api/system/article";
import Template from "../mailTemplate";
import Detail from '@/views/system/article/detail.vue';

export default {
  name: "Report",
  dicts: ['report_type'],
  components: {Template,Detail},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章举报表格数据
      reportList: [],
      detailVis:false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        articleId: null,
        customerId: null,
        reportType: null,
        reportInfo: null,
        reportScreenshot: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      detailObj:{}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询文章举报列表 */
    getList() {
      this.loading = true;
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        articleId: null,
        customerId: null,
        reportType: null,
        reportInfo: null,
        reportScreenshot: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文章举报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReport(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文章举报";
      });
    },
    openDetail(row){
      getArticle(row.articleId).then(res=>{
        this.detailObj=res.data
        this.detailVis=true
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delReport(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/report/export', {
        ...this.queryParams
      }, `report_${new Date().getTime()}.xlsx`)
    },
    changeStatus(row){
      this.$confirm(`确定要${row.hidden=='0'?'隐藏':'开放'}吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        hideArticle({id:row.articleId}).then((res)=>{
          this.$message({
            type: 'success',
            message: '操作成功'
          });
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消'
        });
      });
    },
  }
};
</script>
